<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib/blockad.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=320, initial-scale=1, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<title></title>
<style type="text/css">
*{
	font-family: "Heiti SC","Hiragino Sans GB";
	color: #555;
}
body{
	margin:0;
	background-color:#EEE;
	font-size:12px;
}
.hide{
	display: none;
}
.blank{
	height: 44px;
	width: 100%;
}
.bottom-bar{
	position: fixed;
	bottom: 0;
	width: 100%;
	height: 44px;
}
.submit-text{
	color: #FFF;
	width: 100%;
	height: 100%;
	background-color: #F02800;
	font-size: 15px;
	line-height: 44px;
	text-align: center;
}
.result{
	width:100px;
	height:auto;
	margin:50px auto;
	font-size:18px;
	color:#888;
}
.result img{
	width:100px;
	opacity:0.4;
}
.result div{
	text-align:center;
}
.disable{
	background-color: #999;
}
</style>
</head>
<body>
<?php
//http://127.0.0.1/app/diy_price.php?str=eyJpZCI6MjMsInNldF90eXBlIjowLCJpdGVtcyI6W3sibWF0X2lkIjoyMTkyLCJmYWJfaWQiOjAsImlkIjoyNjc5fSx7Im1hdF9pZCI6MCwiZmFiX2lkIjowLCJpZCI6MjY3NX1dLCJ0eXBlIjoxLCJ1c2VyX2lkIjoyMDA5LCJ1c2VyX3Rva2VuIjp7InNpZCI6IjM3NjcwMiIsInRva2VuIjoiOWMwOGE3Y2VlYWNmMzY3YjMwYTQ3MjA4YjhiZDkyYTA3NTE0MWMifX0=
// {
// 	"set_type":0		//0:单品或DIY，1：套装
// 	"id":23,			//套装或DIY的id
// 	"user_id":2010,
// 	"user_token":{"sid":"376702","token":"9c08a7ceeacf367b30a47208b8bd92a075141c"},
// 	"type":2,			//1加入购物车，2直接购买
// 	"items":[
// 		{"id":2659,"fab_id":0,"mat_id":2171},//id为item的id，材质面料如果未选则值为0
// 		{"id":2660,"fab_id":0,"mat_id":2172},
// 		{"id":2654,"fab_id":0,"mat_id":2168,
// 		{"id":2644,"fab_id":1769,"mat_id":0},
// 		{"id":2691,"fab_id":0,"mat_id":2202}
// 	]
// }
require_once("config/app_webview_config.php");
require_once("app-component.php");
$str=$_GET["str"];
$get_info=base64_decode($str);
$get_info=json_decode($get_info,true);
$user_id=$get_info['user_id'];
$user_token=$get_info['user_token']['token'];
$set_type=$get_info['set_type'];
// print_r($get_info);die;
$sql="SELECT * FROM user_diy WHERE id=".intval($get_info['id'])." AND user_id=".intval($get_info['user_id']);
$res=mysqli_query($conn,$sql);
$diy_info=mysqli_fetch_array($res);
if (!verify($user_id,$user_token)) {
	echo "请重新登录";
	return;
}

createItemWrapperBefore();
$t=$get_info['items'];
foreach ($t as $value) {
	createItemWrapper(new Item($value['id']),false,['mat'=>$value['mat_id'],'fab'=>$value['fab_id']]);
	$sql2="SELECT class_id,brand_id FROM sys_item WHERE id=".$value['id']."";
	$arr[]=mysqli_fetch_array(mysqli_query($conn,$sql2));
}
createItemWrapperAfter();
?>
<div class="blank"></div>
<div class="hide"></div>
<div class="bottom-bar">
	<div class="submit-text" onclick="submit()">确定</div>
</div>
<script type="text/javascript">
function submit()
{
	var submit_type=<?= $get_info['type']?>;
	var post_order={};
		post_order['diy']=[];
		post_order['diy'][0]={};
		post_order['diy'][0]['info']={};
		post_order['diy'][0]['items']=[];
		post_order['diy'][0]['info']={};
		post_order['diy'][0]['items']=[];
		post_order['diy'][0]['info']['id']=<?= $get_info['id']?>;
		post_order['diy'][0]['info']['cart_type']=0;
	var wrappers=document.querySelectorAll(".item-wrapper");
	var i,j,k,rows,count=0;
	var pay_order={};
	pay_order['singleItem']=[];
	for(i=wrappers.length-1;i>=0;i--)
	{
		post_order['diy'][0]['items'][i]={};
		post_order['diy'][0]['items'][i]['sku_id']={};
		post_order['diy'][0]['items'][i]['item_id']=wrappers[i].dataset.itemid;
		post_order['diy'][0]['items'][i]['price_id']=wrappers[i].dataset.priceid;
		post_order['diy'][0]['items'][i]['count']=parseInt(wrappers[i].querySelector(".c").value);
		var arr=<?php echo (json_encode($arr));?>;
		pay_order['singleItem'].push({"info":{"cart_id":0,"cart_type":0},"items":[{"count":parseInt(wrappers[i].querySelector(".c").value),"item_id":wrappers[i].dataset.itemid,"price_id":wrappers[i].dataset.priceid,"class_id":arr[i]['class_id'],"brand_id":arr[i]['brand_id'],"count":"1","sku_id":post_order['diy'][0]['items'][i]['sku_id']}]});
		rows=wrappers[i].querySelectorAll(".fields-opts");
		for(j=rows.length-1;j>=0;j--)
		{
			if(rows[j].querySelector(".opt.selected"))
			{
				post_order['diy'][0]['items'][i]['sku_id'][rows[j].dataset.key]=rows[j].querySelector(".opt.selected").dataset.id;
			}
			else
			{
				wrappers[i].querySelector(".wrapper-warning").classList.remove("hide");
				return false;
			}
		}
		
	}
	post_order['diy'][0]['info']['count']=1;
	if (submit_type==2) {//直接购买
		// post_order['diy'][0]['info']['cart_id']=0;
		// post_order['singleItem']=post_order['diy'];
		// delete post_order.diy;
		var form=document.createElement('form');
		form.action=<?= "'order.php'"?>;
		form.method='post';

		var input1=document.createElement('input');
		input1.name='obj';
		input1.value=JSON.stringify(pay_order);
		var input2=document.createElement('input');
		input2.name="user_id";
		input2.value=<?= $user_id?>;

		var input3=document.createElement('input');
		input3.name="user_token";
		input3.value=<?= "'".$user_token."'"?>;

		form.appendChild(input1);
		form.appendChild(input2);
		form.appendChild(input3);
		document.querySelector(".hide").appendChild(form);
		form.submit();
	}
	else if(submit_type==1){//添加到购物车
		var fd=new FormData();
		fd.append("q","submitShopingCart");
		fd.append("order",JSON.stringify(post_order['diy'][0]));
		fd.append("type",0);
		fd.append("user_id",<?= $user_id?>);
		fd.append("user_token",<?= "'".$user_token."'"?>);

		xhr=new XMLHttpRequest();
		xhr.onreadystatechange = function(){
		  if (xhr.readyState === 4&&xhr.status === 200){
		   if(parseInt(xhr.responseText))
		   {
		   		html="<div class=\"result\"><div><img src=\"images/success.png\"></div><div>提交成功</div></div>";
			   	setTimeout('location.href="webView://close"',1500);
		   }
		   else
		   {
			   html="<div class='result'><div><img src='images/failed.png'></div><div>提交失败<br>重新选择</div></div>";
			   setTimeout('history.back()',1500);
		   }
		   document.body.innerHTML=html;
		  }
		};
		xhr.onerror = function (e) {
		  console.error(xhr.statusText);
		};
		xhr.open("post","calc.php", true);
		xhr.send(fd);	
	}
}
</script>
</body>
</html>